<script setup>
import { ref, onMounted } from 'vue'
import { selectAllDeptInfo, addDept, selectDeptById, deleteDeptById, updateDept } from '../../api/dept.js'
import { ElMessage, ElMessageBox } from 'element-plus'

import axios from 'axios'

// 对话框
const title = ref('')
const dialogFormVisible = ref(false)
const dept = ref({ name: '' })
// 部门数据
const deptInfo = ref([])

// 表单校验值
const deptFormRef = ref()
// 表单校验规则
const rules = ref({
  name: [
    { required: true, message: '请填入部门名称', trigger: 'blur' },
    { min: 2, max: 10, message: '部门名称长度在 2 到 10 个字符', trigger: 'blur' },
  ],
})

onMounted(() => {
  search()
})
// 清空校验值
function resetdeptFormRef() {

  if (deptFormRef.value) {
    deptFormRef.value.resetFields();
  }
}
// 添加按钮函数 用于展示对话框
function addButton() {
  dialogFormVisible.value = true
  dept.value.name = ''
  title.value = '新增部门'

}

// 编辑按钮函数 用于展示对话框
function editButton() {
  dialogFormVisible.value = true
  dept.value.name = ''
  title.value = '编辑部门'


}


// 保存函数 向服务器添加、修改部门信息
async function save() {

  deptFormRef.value.validate(async (valid) => {
    let result
    if (valid) {

      if (dept.value.id) {
        result = await updateDept(dept.value);
      } else {
        result = await addDept(dept.value)
      }
        if (!result.code == 0) {
          ElMessage.success('操作成功！')
          dialogFormVisible.value = false
          search()
        } else {
          ElMessage.error(result.msg)
        }
    } else {
      ElMessage.error('表单校验不通过')
    }
  })
}

// 查询函数 用于向接收服务器返回的所有部门信息
async function search() {
  const result = await selectAllDeptInfo()

  if (result.code) {
    deptInfo.value = result.data
  }
}

// 查询函数 根据Id查询部门信息
async function searchById(id) {
  const result = await selectDeptById(id)
  if (result.code != 0) {
    dept.value = result.data
  }
}

// 删除函数 用于向服务器发送要删除的Id
async function deleteById(id) {
  ElMessageBox.confirm('您确定要删除部门吗?', '删除部门',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(async () => {
    const result = await deleteDeptById(id)
    if (result.code) {
      ElMessage.success("删除成功")
      search()
    } else {
      ElMessage.error("删除失败: " + result.msg)
    }
  }).catch(() => {
    ElMessage.info("取消删除")
  })
}




</script>

<template>
  <h1>部门管理</h1>
  <div class="button">
    <!-- 按钮 -->
    <el-button type="primary" @click="addButton">
      <el-icon style="margin-right: 20px;">
        <Plus />
      </el-icon>
      新增部门
    </el-button>
  </div>

  <!-- 表格 -->
  <div class="table">
    <el-table :data="deptInfo" border style="width: 100%">

      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="name" label="名称" width="180" align='center' />
      <el-table-column prop="createTime" label="更新时间" width="180" align='center' />
      <el-table-column prop="updateTime" label="创建时间" align='center' />
      <el-table-column label="操作" align='center'>
        <template #default="scope">
          <el-button size="small" type="primary" @click="editButton(); searchById(scope.row.id)">
            <el-icon>
              <EditPen />
            </el-icon>编辑
          </el-button>
          <el-button size="small" type="danger" @click="deleteById(scope.row.id)">
            <el-icon>
              <Delete />
            </el-icon>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <!-- 对话框 -->
  <el-dialog v-model="dialogFormVisible" :title=title width="500">
          <!-- {{ dept }} -->
    <!-- 对话框表单 -->
    <el-form :model="dept" :rules="rules" ref="deptFormRef">
      <!-- 表单项 -->
      <el-form-item label="部门名称" label-width=80px prop="name">
        <el-input v-model="dept.name" />
      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false; resetdeptFormRef()">取消</el-button>
        <el-button type="primary" @click="save"> 保存 </el-button>
      </div>
    </template>
  </el-dialog>





</template>

<style scoped>
.button {
  margin: 20px 20px;
}

.table {
  width: 80%;
  margin: 30px 20px;
}
</style>
